package com.LeeCode;

import java.util.Arrays;

/**
 * 最高频元素的频数
 */

public class Code1838 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 4};
        int k = 5;
        System.out.println(new Code1838().maxFrequency(nums, k));
    }

    public int maxFrequency(int[] nums, int k) {
        Arrays.sort(nums);

        int left = 0, ans = 1;
        long cost = 0;
        for (int i = 1; i < nums.length; i++) {
            cost += (long) (i - left) * (nums[i] - nums[i - 1]);

            while (cost > k) {
                cost -= nums[i] - nums[left];
                left++;
            }

            ans = Math.max(ans, i - left + 1);
        }
        return ans;
    }
}
